/**
 * Copyright (C) 2011 PROCESSBASE Ltd.
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GPL as published by the Free Software Foundation version 3 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, see <http://www.gnu.org/licenses/>.
 */
package org.processbase.touchprocess.ui.template;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Window.Notification;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ow2.bonita.facade.def.majorElement.DataFieldDefinition;
import org.ow2.bonita.facade.runtime.AttachmentInstance;
import org.ow2.bonita.facade.runtime.Document;
import org.ow2.bonita.facade.runtime.InitialAttachment;
import org.ow2.bonita.facade.runtime.TaskInstance;
import org.ow2.bonita.light.LightProcessDefinition;
import org.ow2.bonita.light.LightProcessInstance;
import org.processbase.touchprocess.ui.TouchProcessApplication;
import org.processbase.touchprocess.ui.generator.BarResource;
import org.processbase.ui.core.BPMModule;
import org.processbase.ui.core.bonita.forms.FormsDefinition;
import org.processbase.ui.core.bonita.forms.PageFlow;
import org.processbase.ui.core.bonita.forms.Widget;

/**
 *
 * @author marat
 */
public class DetailsLayout extends CssLayout implements Button.ClickListener {
    
    protected TaskInstance task = null;
    protected LightProcessDefinition processDefinition;
    protected LightProcessInstance processInstance;
    protected BPMModule bpmModule = null;
    protected String currentUserName = null;
    
    protected Map<String, Object> processInstanceVariables = new HashMap<String, Object>();
    protected Map<String, Object> activityInstanceVariables = new HashMap<String, Object>();
    protected Map<String, DataFieldDefinition> processDataFieldDefinitions = new HashMap<String, DataFieldDefinition>();
    protected Map<String, DataFieldDefinition> activityDataFieldDefinitions = new HashMap<String, DataFieldDefinition>();
    
    protected BarResource barResource;
    protected FormsDefinition formsDefinition;
    protected PageFlow pageFlow;
    protected HashMap<Component, Widget> componentWidgets = new HashMap<Component, Widget>();
    protected HashMap<String, Component> fields = new HashMap<String, Component>();
    protected ArrayList<GridLayout> pages = new ArrayList<GridLayout>();
    protected int currentPage = 0;
    protected List<AttachmentInstance> attachmentInstances = null;
    protected ArrayList<InitialAttachment> initialAttachments = new ArrayList<InitialAttachment>();
    protected Map<Document, byte[]> attachmentBodies = new HashMap<Document, byte[]>();
    protected Map<String, Document> attachmentsDefinitions = new HashMap<String, Document>();
    protected Map<String, Object> groovyScripts = new HashMap<String, Object>();

    
    public void initUI(){
        bpmModule = TouchProcessApplication.getCurrent().getBpmModule();
        currentUserName = TouchProcessApplication.getCurrent().getUserName();
        prepareVariables();
    }

    public BarResource getBarResource() {
        return barResource;
    }

    public void setBarResource(BarResource barResource) {
        this.barResource = barResource;
    }

    public LightProcessDefinition getProcessDefinition() {
        return processDefinition;
    }

    public void setProcessDefinition(LightProcessDefinition processDefinition) {
        this.processDefinition = processDefinition;
    }

    public TaskInstance getTask() {
        return task;
    }

    public void setTask(TaskInstance task) throws Exception {
        this.task = task;
//        this.processInstance = TouchProcessApplication.getCurrent().getBpmModule().getProcessInstance(task.getProcessInstanceUUID());
        this.processDefinition = TouchProcessApplication.getCurrent().getBpmModule().getProcessDefinition(task.getProcessDefinitionUUID());
    }
    
    private void prepareVariables() {
        try {
            if (task != null) {
                for (DataFieldDefinition dfd : bpmModule.getProcessDataFields(task.getProcessDefinitionUUID())) {
                    processDataFieldDefinitions.put(dfd.getName(), dfd);
                }
                for (DataFieldDefinition dfd : bpmModule.getActivityDataFields(task.getActivityDefinitionUUID())) {
                    activityDataFieldDefinitions.put(dfd.getName(), dfd);
                }
                processInstanceVariables.putAll(bpmModule.getProcessInstanceVariables(task.getProcessInstanceUUID()));
                activityInstanceVariables.putAll(bpmModule.getActivityInstanceVariables(task.getUUID()));
            } else {
                for (DataFieldDefinition dfd : bpmModule.getProcessDataFields(processDefinition.getUUID())) {
                    processDataFieldDefinitions.put(dfd.getName(), dfd);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void buttonClick(ClickEvent event) {
       
    }
    
    public void showError(String description) {
        showMessage(description, Notification.TYPE_ERROR_MESSAGE);
    }

    public void showInformation(String description) {
        showMessage(description, Notification.TYPE_HUMANIZED_MESSAGE);
    }

    public void showWarning(String description) {
        showMessage(description, Notification.TYPE_WARNING_MESSAGE);
    }
    
    public void showImportantInformation(String description) {
        StringBuilder desc = new StringBuilder();
        if (description != null) {
            int i = 0;
            for (; description.length() > i + 50; i = i + 50) {
                desc.append("<br/> ").append(description.substring(i, i + 50));
            }
            desc.append("<br/> ").append(description.substring(i));
        } else {
            desc.append("<br/> null");
        }
        getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("informationCaption"), desc.substring(0), Notification.TYPE_ERROR_MESSAGE);

    }

    public void showMessage(String description, int type) {
        StringBuilder desc = new StringBuilder();
        if (description != null) {
            int i = 0;
            for (; description.length() > i + 50; i = i + 50) {
                desc.append("<br/> ").append(description.substring(i, i + 50));
            }
            desc.append("<br/> ").append(description.substring(i));
        } else {
            desc.append("<br/> null");
        }
        switch (type) {
            case Notification.TYPE_WARNING_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("warningCaption"), desc.substring(0), type);
                break;
            case Notification.TYPE_HUMANIZED_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("informationCaption"), desc.substring(0), type);
                break;
            case Notification.TYPE_ERROR_MESSAGE:
                getWindow().showNotification(TouchProcessApplication.getCurrent().getPbMessages().getString("exceptionCaption"), desc.substring(0), type);
                break;
        }
    }
    
}
